// Get executing rooms of a service
function ajaxRoomFilter(service) {  
    updatePrice(service);
        
    $.ajax ({
        type:       "GET",
        url:        '../Service/FilterRoomAjax?set_ajax_view',
        data:       'service_id=' + service.val(),
        beforeSend: function() {
            $('input#add_service').attr('disabled', 'disabled');
        },
        complete: function() {
                $('input#add_service').removeAttr('disabled');
        },
        success: function(data) {           
            $('select#service_room').html(data);                            
        }
    });         
}

function updatePrice(service) {
    if ($('#ticket_id').val() > 0) {
        $.ajax ({
            type:       "GET",
            url:        '../Service/getPatientPriceAjax?set_ajax_view',
            data:       'service_id=' + service.val() +
                        '&ticket_id=' + $('#ticket_id').val(),                          
            success: function(data) {
                $('#service_price').text(data);
            }
        }); 
    }   
}

// Get services by department
function ajaxServiceFilter(department) {
    $.ajax ({
        type:       "GET",
        url:        '../Department/FilterServiceAjax?set_ajax_view',
        data:       'department_id=' + department.val(),
        beforeSend: function() {
            $('input#add_service').attr('disabled', 'disabled');
        },      
        success: function(data) {
            $('select#service').html(data);         
            ajaxRoomFilter($('select#service'));                                
        }
    }); 
}


function addServiceAjax() {
    $.ajax({
        url:        "../Ticket/CheckReadyToAddServiceAjax?set_ajax_view",
        data:       "ticket_id="        + $('input#ticket_id').val() +
                    "&service_room_id=" + $('select#service_room').val(),
        dataType:   "json",                         
        success:    function(result) {
            // ticket is closed
            if (result.status == 0) {
                cannotDo(result.body);
                return;
            }                           
            
            // duplicated service
            if (result.status == 1) {
                promptConfirm('', result.body, function() {                                         
                        addServiceSubmit();
                        return true;
                        },'NextCancel','',_('Cancel'));
            }
            
            // ready
            if (result.status == -1) {
                addServiceSubmit();
            }                       
        }
    });  
}

function cannotDo(message) {
    //button close  
    var buttons = {};
    buttons[_('Close')] = function() {
            $(this).dialog('close');        
        };
    generalDialog(null,null,message,null,{
            autoOpen: true,         
            bgiframe: true,
            resizable: true,
            modal: true,                            
            buttons: buttons
        },_('Close'));
}

function addServiceSubmit() {
    
    $.ajax ({
        type:       "GET",
        url:        "../Ticket/AddServiceAjax?set_ajax_view",
        data:       "ticket_id="        + $('input#ticket_id').val() +
                    "&service_room_id=" + $('select#service_room').val(),                   
        beforeSend: function() {
            $('input#add_service').attr('disabled', 'disabled');            
            $('div#loading').show('fast');
        },
        complete:   function() {
            $('input#add_service').removeAttr('disabled');
            $('div#loading').hide('slow')
        },
        success:    function(result) {                                  
            viewOpenTicket($('#ticket_id').val());                                          
        }
    });
}
 
function addCompositeService(){
    var compositeId = $('select#composite_service').val();
    // PRECHECK
    $.ajax({
        url:        "../Ticket/CheckReadyToAddCompositeServiceAjax?set_ajax_view",
        data:       "ticket_id=" + $('#ticket_id').val() +
                    "&composite_service_id="    + compositeId,
        dataType:   "json",                         
        success:    function(result) {
            // ticket is closed
            if (result.status == 0) {
                cannotDo(result.body);
                return;
            }                           
                
            // duplicated service
            if (result.status == 1) {
                promptConfirm('', result.body, function() {                                         
                   addCompositeServiceAjax();
                   return true;
                   },'NextCancel','',_('Cancel'));
           }
                    
            // empty composite service
            if (result.status == 2) {
                //no button
                generalDialog(null,null,result.body);
            }                   
                    
            // ready
            if (result.status == -1) {
                addCompositeServiceAjax();
            }                       
        }
    });   
}

function addCompositeServiceAjax() {                        
    $.ajax ({
        type:   "GET",
        url:    "../Ticket/AddCompositeServiceAjax?set_ajax_view",
        data:   "ticket_id=" +  $('input#ticket_id').val() + 
                "&composite_service_id=" + $('select#composite_service').val(),
        beforeSend: function() {
            $('#add_composite_service').attr('disabled', 'disabled');                   
            $('div#loading').show();
            $('div#add_composite_service_dialog').dialog('close');
        },
        complete: function(){   
            $('#add_composite_service').removeAttr('disabled');                             
            $('div#loading').hide('fast');                  
        },
        success: function(result) {                             
            //$('table#ticket_item tbody').append(result);
            viewOpenTicket($('#ticket_id').val());                                              
        }
    });         
}

function viewOpenTicket(ticketId) {
    $.ajax({
        type:       "GET",
        url:        "../Ticket/ViewOpenTicketAjax?set_ajax_view",
        data:       "ticket_id=" + ticketId,
        dataType:   "json",
        beforeSend: function() {                        
            $('div#loading').show();            
        },
        complete:   function() {
            $('div#loading').hide();            
        },
        success:    function(data) {                     
            if (data.ticket_body == "") {
                $('table#ticket_item').hide();
                return true;
            } else {
                $('table#ticket_item tbody').html(data.ticket_body);
                styleTicketTable();                                
                $('table#ticket_item').show();
            }               
        }       
    });
}

function styleTicketTable() {
    $('#ticket_item tr').removeClass('last');
    $('#ticket_item tr:last').addClass('last');
}

function addParaclinicalService() {
    var enumParaClinic = $('select#paraClinicService').val();
    $.ajax({
        url:        "../Ticket/RenderParaclinicAssignmentAjax?set_ajax_view",
        data:       "ticket_id=" + $('#ticket_id').val()
                    + "&enumParaClinic=" + enumParaClinic,
        dataType:   "json",                         
        success:    function(data) {
            if (data.error.code ==  'OK') {
                $('#paraClinicAssignment').html(data.result);                       
            } else {
                displayError(data.error.mess);              
            }                                           
        }
    });
}

function updateCompositeServiceAjax(){
	var queryString = "";
	var inputArray = new Array();
	$('#update_composite_service_dialog').find('input.composite_service_item_popup').each(function() {          
	    if ($(this).is(":checked")) {           
	        queryString += "&composite_service_item[]=" + $(this).attr('value');
	        inputArray.push($(this).attr('value'));
	    }
	});
	
	if (inputArray.length == 0) {
	    $('div#update_composite_service_dialog').dialog('close');
	    return false;
	}   
	
	$.ajax ({
	    type:   "GET",
	    url:    "../Ticket/UpdateCompositeServiceAjax?set_ajax_view",
	    data:   "ticket_id=" +  $('input#ticket_id').val() + 
	            "&ticket_item_id=" + $('input#update_ticket_item_id').val() +
	            "&composite_service_id=" + $('input#composite_service_id').val() + queryString,
	    beforeSend: function() {
	        $('#update_composite_service').attr('disabled', 'disabled');                    
	        $('div#loading').show('fast');
	        $('div#update_composite_service_dialog').dialog('close');
	    },
	    complete: function(){   
	        $('#update_composite_service').removeAttr('disabled');                              
	        $('div#loading').hide('fast');                  
	    },
	    success: function(result) {                             
	        //$('table#ticket_item tbody').append(result);
	        viewOpenTicket($('#ticket_id').val());                                              
	    }
	});
}

function popUpCompositeService(compositeId) {
	$.ajax({
	    type:       "GET",
	    url:        "../CompositeService/ListItemAjax?set_ajax_view",
	    data:       "composite_service_id=" + compositeId + 
	                "&ticket_id=" + $('#ticket_id').val(),
	    beforeSend: function() {
	        $('#add_composite_service').attr('disabled', 'disabled');
	        $('div#loading').show('fast');
	    },
	    complete: function(){           
	        $('#add_composite_service').removeAttr('disabled');
	        $('div#loading').hide('fast');
	    },
	    success:    function(data) {                    
	        $('div#add_composite_service_dialog').find('table#composite_service_item_data').remove();
	        $('div#add_composite_service_dialog').find('input#composite_service_id').remove();
	        $('div#add_composite_service_dialog').append(data);             
	        var buttons = {};
	        buttons[_('Add')] = function() {
	            addCompositeServiceAjax();
	            $(this).dialog('close');        
	        };
	        generalDialog('add_composite_service_dialog',null,null,null,{
	            autoOpen: true,         
	            bgiframe: true,
	            resizable: true,        
	            width: 700,
	            height: 400,
	            modal: true,
	            buttons:buttons
	        });
	    }
	});
}

function labAssignmentDetail(ticketId, labAssignmentTicketId) { 
    var idOfActionAfterUpdating = 'refreshParaClinic';
    $.ajax({
        type:       "GET",
        url:        "../Ticket/BuildLabAssignment4VEditAjax?set_ajax_view",
        data:       "ticketId=" + ticketId 
        + "&idOfActionAfterUpdating=" + idOfActionAfterUpdating
        + "&labAssignmentTicketId=" + labAssignmentTicketId,        
        dataType:   "json",
        success:    function(data) {

        if (data.error.code ==  'OK') {
            $("div#paraClinicAssignment").html(data.result);            

        } else {
            displayError(data.error.mess);              
        }

    }
    }); 
}

function radAssignmentDetail(radTicketId, enumRadType) {
	var idOfActionAfterUpdating = 'refreshParaClinic';
	$.ajax({
	     type:       "GET",
	     url:        "../Ticket/LoadRadAssignmentEditAjax?set_ajax_view",
	     data:       "radTicketId=" + radTicketId + "&radType=" + 
	                 enumRadType + "&triggeredElement=" + idOfActionAfterUpdating,  
	     dataType:   "json",
	     success:    function(data) {
	         if (data.error.code ==  'OK') {
	             $("div#paraClinicAssignment").html(data.result);
	         } else {
	             displayError(data.error.mess);              
	         }
	     }
	 });
}